package com.zlx.mall.basic.web.service;

import com.zlx.mall.basic.util.json.JacksonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author qingke
 **/
public abstract class AbstractHttpService implements IHttpService {

    @Resource
    protected IHttpLogService httpLogService;

    protected String getHeaders(HttpEntity<?> httpEntity) {
        return Optional
                .ofNullable(httpEntity)
                .map(HttpEntity::getHeaders)
                .map(JacksonUtil::toJsonString)
                .orElse(StringUtils.EMPTY);
    }

    protected String getBody(HttpEntity<?> httpEntity) {
        return Optional
                .ofNullable(httpEntity)
                .map(HttpEntity::getBody)
                .map(body -> {
                    if (body instanceof String) {
                        return (String) body;
                    } else {
                        return JacksonUtil.toJsonString(body);
                    }
                })
                .orElse(StringUtils.EMPTY);
    }

}
